Date.prototype.format = function (format) {
	var o = {
		"M+": this.getMonth() + 1, // month
		"d+": this.getDate(), // day
		"H+": this.getHours(), // hour
		"m+": this.getMinutes(), // minute
		"s+": this.getSeconds(), // second
		"q+": Math.floor((this.getMonth() + 3) / 3), // quarter
		"S": this.getMilliseconds() // millisecond
	}
	if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
	}
	return format;
}

/* 格式化数字 */
function formatNumber(value, precision) {
	if (isNaN(value)) {
		return value;
	}
	var fTmp = 0.00;// 临时变量
	var iFra = 0;// 小数部分
	var iInt = 0;// 整数部分
	var aBuf = new Array();
	var bPositive = true;// 保存正负值标记
	function funZero(iVal, iLen) {
		var sTmp = iVal.toString();
		var sBuf = new Array();
		for (var i = 0, iLoop = iLen-sTmp.length; i < iLoop; i++) { sBuf.push('0'); }
		sBuf.push(sTmp);
		return sBuf.join('');
	}
	if (!precision) precision = 0;
	bPositive = (value >= 0);
	fTmp = (isNaN(fTmp = parseFloat(value))) ? 0 : Math.abs(Math.round(fTmp * Math.pow(10, precision)) / Math.pow(10, precision));
	iInt = parseInt(fTmp);
	iFra = parseInt((fTmp * Math.pow(10, precision) - iInt * Math.pow(10, precision)) + 0.1);
	do {
		aBuf.unshift(funZero(iInt%1000, 3));
	} while((iInt = parseInt(iInt/1000)));
	aBuf[0] = parseInt(aBuf[0],10).toString();
	if (precision) {
		return ((bPositive)?'':'-') + aBuf.join(',') + '.' + funZero(iFra, precision);
	} else {
		return ((bPositive)?'':'-') + aBuf.join(',');
	}
}

/* 列表中日期格式化 */
function formatDate(value, formatString) {
	if (!value) {
		return '';
	}
	var dt;
	if (value instanceof Date) {
		dt = value;
	} else if (typeof value === 'string') {
		dt = new Date(value.replace(/-/g,'/').replace(/T|Z/g,' ').trim());
	} else {
		dt = new Date(value);
	}
	if (arguments.length == 2)
		return dt.format(formatString);
	else
		return dt.format("yyyy-MM-dd");
}

function dateFormatter(value) {
	return formatDate(value, "yyyy-MM-dd");
}

function dateTimeFormatter(value) {
	return formatDate(value, "yyyy-MM-dd HH:mm:ss");
}

/* checkbox格式化 */
function checkboxFormatter(value) {
	if (value == true || value == "true" || value == "on") {
		return '&radic;';
	} else {
		return '';
	}
}

/* 百分号 */
function percentFormatter(value) {
	if (value == null)
		return '';
	return value + '%';
}

/* 数字格式化 */
function numberFormatter(value) {
	if (value == null)
		return '';
	return formatNumber(value, 4);
}

/* 金额格式化 */
function moneyFormatter(value) {
	if (value == null)
		return '';
	return formatNumber(value, 2);
}

/* 整数 */
function intFormatter(value) {
	if (value == null)
		return '';
	return formatNumber(value, 0);
}

// Tooltip
function cellTooltipFormatter(value) {
	if (value) {
		return '<span title= "' + textContentFormatter(value.replace(/\s+/g," ")) + '">' + textContentFormatter(value) + '</span>';
	} else {
		return '';
	}
}

//输出显示html标签
function textContentFormatter(s) {
	var re = /"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g;
	return (typeof s != "string") ? s :
		s.replace(re, function ($0) {
			var c = $0.charCodeAt(0), r = ["&#"];
			c = (c == 0x20) ? 0xA0 : c;
			r.push(c);
			r.push(";");
			return r.join("");
		});
}
